/*
 * @author: Victor
 * @Date: 2021-08-19 19:34:16
 * @LastEditTime: 2021-08-19 20:47:35
 */
$(function () {
  var layer = layui.layer;
  // 获取用户基本信息
  getUserInfo();
  // 退出
  $('#btn-logout').click(function () {
    layer.confirm(
      '确定退出登录?',
      { icon: 3, title: '提示' },
      function (index) {
        localStorage.removeItem('token');
        location.href = 'login.html';
        layer.close('index');
      }
    );
  });
});
function getUserInfo() {
  $.ajax({
    type: 'GET',
    url: '/my/userinfo',
    success: function (res) {
      if (res.status != 0) {
        return layer.msg('获取用户信息失败!');
      }
      renderAvatar(res.data);
    },
  });
}
// 渲染头像
function renderAvatar(user) {
  // 获取昵称/用户名
  var username = user.nickname || user.username;
  // 若有用户头像显示头像，没有时显示
  if (user.user_pic !== null) {
    $('.user-img')
      .prop('src', user.user_pic)
      .show()
      .siblings('.text-avatar')
      .hide();
  } else {
    $('.user-img')
      .hide()
      .siblings('.text-avatar')
      .html(username[0].toUpperCase())
      .show();
  }
  $('.welcome').html('欢迎' + username);
}
